﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace RollDesktop
{
    public class ControlProcess
    {
        public string OutPutMessage { get; set; }
        public void StartCMDProcess(string[] commands, string arguments = null, string fileName = "cmd.exe", string workingDirectory = null)
        {
            Process pro = new Process();
            pro.StartInfo.FileName = fileName;
            if (!string.IsNullOrEmpty(workingDirectory))
            {
                pro.StartInfo.WorkingDirectory = workingDirectory;
            }
            if (!string.IsNullOrEmpty(arguments))
            {
                pro.StartInfo.Arguments = arguments;
            }
            pro.StartInfo.Verb = "RunAs";
            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.RedirectStandardInput = true;
            pro.StartInfo.RedirectStandardOutput = true;
            pro.StartInfo.RedirectStandardError = true;
            pro.StartInfo.CreateNoWindow = true;
            pro.Start();
            for (int i = 0; i < commands.Length; i++)
            {
                pro.StandardInput.WriteLine(commands[i]);
            }
            pro.StandardInput.WriteLine("exit");
            OutPutMessage = pro.StandardOutput.ReadToEnd();
            pro.Close();
        }
    }
}
